﻿namespace Hidistro.UI.Web.Admin.promotion.Ascx
{
    using ASPNET.WebControls;
    using Hidistro.Entities.Promotions;
    using Hidistro.UI.ControlPanel.utility;
    using kindeditor.Net;
    using System;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.HtmlControls;
    using System.IO;
    using Hidistro.UI.Common.Controls;
    using Hidistro.ControlPanel.Promotions;
    using System.Web;

    public class PromotionView : UserControl
    {
        protected WebCalendar calendarEndDate;
        protected WebCalendar calendarStartDate;
        protected MemberGradeCheckBoxList chklMemberGrade;
        protected KindeditorControl fckDescription;
        private PromotionInfo promotion;
        protected TextBox txtPromoteSalesName;
        protected HtmlGenericControl wnxd_ProductList;
        protected ImageLinkButton btnDeleteAll;
        protected wnxd_ProductList wnxd_pl;
        protected void Page_Load(object sender, EventArgs e)
        {
            this.wnxd_ProductList = (HtmlGenericControl)this.FindControl("wnxd_ProductList");
            this.wnxd_pl = (wnxd_ProductList)this.FindControl("wnxd_pl");
            this.wnxd_pl.activityId = this.activityId;
            this.btnDeleteAll.Click += new EventHandler(this.btnDeleteAll_Click);
            if (Path.GetFileName(this.Page.Request.PhysicalPath) != "EditOrderPromotion.aspx") this.wnxd_ProductList.Visible = false;
            if (!this.Page.IsPostBack)
            {
                this.chklMemberGrade.DataBind();
                if (this.promotion != null)
                {
                    this.txtPromoteSalesName.Text = this.promotion.Name;
                    this.calendarStartDate.SelectedDate = new DateTime?(this.promotion.StartDate);
                    this.calendarEndDate.SelectedDate = new DateTime?(this.promotion.EndDate);
                    this.chklMemberGrade.SelectedValue = this.promotion.MemberGradeIds;
                    this.fckDescription.Text = this.promotion.Description;
                }
            }
        }

        public PromotionInfo Promotion
        {
            get
            {
                PromotionInfo info = new PromotionInfo
                {
                    Name = this.txtPromoteSalesName.Text
                };
                if (this.calendarStartDate.SelectedDate.HasValue)
                {
                    info.StartDate = this.calendarStartDate.SelectedDate.Value;
                }
                if (this.calendarEndDate.SelectedDate.HasValue)
                {
                    info.EndDate = this.calendarEndDate.SelectedDate.Value.AddHours(23.0).AddMinutes(59.0).AddSeconds(59.0);
                }
                info.MemberGradeIds = this.chklMemberGrade.SelectedValue;
                info.Description = this.fckDescription.Text;
                return info;
            }
            set
            {
                this.promotion = value;
            }
        }
        public int activityId { get; set; }
        private void btnDeleteAll_Click(object sender, EventArgs e)
        {
            if (PromoteHelper.DeletePromotionProducts(this.activityId, null))
            {
                base.Response.Redirect(HttpContext.Current.Request.Url.ToString(), true);
            }
        }
    }
}

